package com.yc.biz.impl;

import com.yc.entity.Mgoods;
import com.yc.entity.Promotion;
import com.yc.entity.Promotiondetail;
import com.yc.mapper.MgoodsMapper;
import com.yc.mapper.PromotionMapper;
import com.yc.biz.IPromotionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxw
 * @since 2025-02-13
 */
@Service
public class PromotionServiceImpl extends ServiceImpl<PromotionMapper, Promotion> implements IPromotionService {
    @Autowired
    PromotionMapper promotionMapper;
    @Autowired
    MgoodsMapper mgoodsMapper;
    @Transactional
    @Override
    public void begin(int pid) {
        Promotion promotion=promotionMapper.queryById(pid);
        for (Promotiondetail promotiondetail:promotion.getDetails()){
            Mgoods mgoods=promotiondetail.getMgoods();
            mgoods.setKucun(mgoods.getKucun()-promotiondetail.getNums());
            mgoodsMapper.updateById(mgoods);

        }
        promotion.setStatus(1);
        promotionMapper.updateById(promotion);
    }
    @Transactional
    @Override
    public void end(Promotion promotions) {
        Promotion promotion=promotionMapper.queryById(promotions.getPid());
        for (Promotiondetail promotiondetail:promotion.getDetails()){
            Mgoods mgoods=promotiondetail.getMgoods();

            Promotiondetail promotiondetail1=promotions.getDetails()
                    .stream()
                    .filter(d->d.getPid().equals(promotiondetail.getPdid()))
                    .collect(Collectors.toList())
                    .get(0);
            //加上剩余的数量
            mgoods.setKucun(mgoods.getKucun()+promotiondetail.getNums());
            mgoodsMapper.updateById(mgoods);

            mgoods.setKucun(mgoods.getKucun()-promotiondetail.getNums());
            mgoodsMapper.updateById(mgoods);

        }
        promotion.setStatus(2);
        promotionMapper.updateById(promotion);
    }
}
